﻿var pageSize = 15;
$(document).ready(function () {
    $("#btnAdd").click(function () {
        Edit(null, null, null, null);
    });
    PageClickFunc(1)
})
PageClickFunc = function (pageclickednumber) {
    InitGetList(pageclickednumber);
}
function InitGetList(pageclickednumber) {
    $.ajax({
        type: "post",
        data: { action: "GetList", pageIndex: pageclickednumber, pageSize: pageSize },
        datatype: "json",
        url: "../../Ajax/CommunicationAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                var pageCount = $.parseJSON(data).flag;
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPid'>";
                    tbody += v.Pid;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += v.CreateTime;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblRemark'>";
                    tbody += decodeURIComponent(v.Remark);
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsUsed'>";
                    tbody += v.IsUsed;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPwd'>";
                    tbody += v.Pwd;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPagingPrencent'>";
                    tbody += v.PagingPrencent;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody)
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });


}

function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}
function Delete(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", name: lbl },
            async: true,
            url: "../../Ajax/CommunicationAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $(contr).parent().parent().remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
function Edit(lbl) {

    var Id = $(lbl).parent().parent().find("lable[name=lblName]").attr("id");
    var pwd = $(lbl).parent().parent().find("lable[name=lblPwd]").text()
    var pagingPrencent = $(lbl).parent().parent().find("lable[name=lblPagingPrencent]").text()
    var content = "";
    if (lbl != null) {

        content += "<table><tr><td>渠道名称：<input type='text' id='txtName' value='" + $(lbl).parent().parent().find("lable[name=lblName]").first().text() + "' style='width:200px;float:right' /></td></tr>";
        content += "<tr><td>渠道Pid:<input type='text' id='txtPid' value='" + $(lbl).parent().parent().find("lable[name=lblPid]").first().text() + "' style='width:200px;float:right' /></td></tr>";
        content += "<tr><td>渠道说明:<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'>" + $(lbl).parent().parent().find("lable[name=lblRemark]").first().text() + "</textarea></td></tr>";
        content += "<tr><td>密码:<input type='text' id='txtPwd'  style='width:200px;float:right' value='" + pwd + "' /></td></tr>";
        content += "<tr><td>分成比例:<input type='text' id='txtPagingPrencent'  style='width:200px;float:right' value='" + pagingPrencent + "' /></td></tr>";
        content += "<tr><td>是否启用:<input type='text' id='txtIsUsed' value='" + $(lbl).parent().parent().find("lable[name=lblIsUsed]").first().text() + "' style='width:200px;float:right' /></td></tr></table>";

    }
    else {
        content = "<table><tr><td>渠道名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>";
        content += "<tr><td>渠道Pid:<input type='text' id='txtPid'  style='width:200px;float:right' /></td></tr>";
        content += "<tr><td>渠道说明:<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'></textarea></td></tr>";
        content += "<tr><td>密码:<input type='text' id='txtPwd'  style='width:200px;float:right' /></td></tr>";
        content += "<tr><td>分成比例:<input type='text' id='txtPagingPrencent'  style='width:200px;float:right' /></td></tr>";
        content += "<tr><td>是否启用:<input type='text' id='txtIsUsed' style='width:200px;float:right' /></td></tr></table>";
    }
    $.dialog({
        title: "添加渠道模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            var objectName = $("#txtName").val();
                            var objectPid = $("#txtPid").val();
                            var objectRemark = $("#txtRemark").val();
                            var objectIsUsed = $("#txtIsUsed").val();
                            if ($.trim(objectName) == "") {
                                $.dialog.alert("请输入模板名称！");
                                return false;
                            }
                            AddMould(lbl, $("#txtName"), Id);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}


function AddMould(contr, sortNum, sortId) {
    var id = sortId;
    var objectName = $(sortNum).parent().parent().parent().find("#txtName").val();
    var objectPid = $(sortNum).parent().parent().parent().find("#txtPid").val();
    var objectRemark = $(sortNum).parent().parent().parent().find("#txtRemark").val();
    var objectIsUsed = $(sortNum).parent().parent().parent().find("#txtIsUsed").val();
    var pwd = $(sortNum).parent().parent().parent().find("#txtPwd").val();
    var pagingPrencent = $(sortNum).parent().parent().parent().find("#txtPagingPrencent").val();
    var data = contr == null ? { action: "Add", name: objectName, Pid: objectPid, Remark: objectRemark, IsUsed: objectIsUsed, pwd: pwd, pagingPrencent: pagingPrencent} :
         { action: "Update", Id: id, name: objectName, Pid: objectPid, Remark: objectRemark, IsUsed: objectIsUsed, pwd: pwd, pagingPrencent: pagingPrencent };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/CommunicationAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != "False") {
                $.dialog.tips("更新成功！", 1);
                var id = data;
                if (contr == null) {
                    var tbody = "<tr>"
                    tbody += "<td><lable name='lblName' id='" + data + "' >";
                    tbody += objectName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPid' >";
                    tbody += objectPid;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += objectName;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblRemark'>";
                    tbody += objectRemark;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsUsed'>";
                    tbody += objectIsUsed;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPwd'>";
                    tbody += pwd;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPagingPrencent'>";
                    tbody += pagingPrencent;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";

                    $("#tb1").append(tbody);
                } else {
                    $(contr).parent().parent().find("lable[name=lblName]").first().text(objectName);
                    $(contr).parent().parent().find("lable[name=lblPid]").first().text(objectPid);
                    $(contr).parent().parent().find("lable[name=lblRemark]").first().text(objectRemark);
                    $(contr).parent().parent().find("lable[name=lblIsUsed]").first().text(objectIsUsed);
                    $(contr).parent().parent().find("lable[name=lblPwd]").first().text(pwd);
                    $(contr).parent().parent().find("lable[name=lblPagingPrencent]").first().text(pagingPrencent);
                }
            }
            else {
                $.dialog.tips("更新失败！", 1);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
}
